﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Microsoft.PlayerFramework
{
    /// <summary>
    /// IValueConverter implementation used to return a unique result depending on boolean input.
    /// </summary>
    public class ConditionalConverter : DependencyObject, IValueConverter
    {
        /// <summary>
        /// Identifies the ValueTrue dependency property.
        /// </summary>
        public static readonly DependencyProperty ValueTrueProperty = DependencyProperty.Register("ValueTrue", typeof(object), typeof(ConditionalConverter), null);

        /// <summary>
        /// Gets or sets the value to return if the input is true.
        /// </summary>
        public object ValueTrue
        {
            get { return GetValue(ValueTrueProperty); }
            set { SetValue(ValueTrueProperty, value); }
        }

        /// <summary>
        /// Identifies the ValueFalse dependency property.
        /// </summary>
        public static readonly DependencyProperty ValueFalseProperty = DependencyProperty.Register("ValueFalse", typeof(object), typeof(ConditionalConverter), null);

        /// <summary>
        /// Gets or sets the value to return if the input is false.
        /// </summary>
        public object ValueFalse
        {
            get { return GetValue(ValueFalseProperty); }
            set { SetValue(ValueFalseProperty, value); }
        }

        /// <inheritdoc /> 
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool bValue;
            if (value is bool)
            {
                bValue = (bool)value;
            }
            else
            {
                if (parameter.GetType() == value.GetType())
                {
                    bValue = value == parameter;
                }
                else
                {
                    bValue = value.ToString() == parameter.ToString();
                }
            }
            return bValue ? ValueTrue : ValueFalse;
        }

        /// <inheritdoc /> 
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
